package ch.csp.transformer;

import java.util.ArrayList;

import ch.csp.exchange.LineInfo;
import ch.csp.exchange.ParagraphWithLineAndWords;
import ch.csp.exchange.ParagraphWithLines;
import ch.csp.exchange.ParagraphWithWords;
import ch.csp.exchange.Words;

public class LineTransformer implements Transformer{
	//Sebe, nimmt Wörter und erstellt die entsprechenden Zeilen
	

	private final ArrayList<ParagraphWithWords> paragraphsWithWords;
	private final int columnWidth;

	public LineTransformer(ArrayList<ParagraphWithWords> paragraphsWithWords, int columnWidth) {
		this.paragraphsWithWords = paragraphsWithWords;
		this.columnWidth = columnWidth;
	}
	
	public ArrayList<ParagraphWithLineAndWords> transform(){
		int lengthCounter = 0;
		String tmpString;
		boolean split;
		
		ArrayList<ParagraphWithLineAndWords> outputList = new ArrayList<ParagraphWithLineAndWords>();
		ArrayList<LineInfo> lineInfoList = new ArrayList<LineInfo>();
		Words wordList = new Words();
		
		for(ParagraphWithWords paragraphWithWord:this.paragraphsWithWords) {
			outputList.add(new ParagraphWithLineAndWords());
			for(String word:paragraphWithWord.words) {
				
				// Check word length
				if(word.length() > this.columnWidth){
					if(lengthCounter != -1) {
						// New Line update Last Line Info List
						lineInfoList.add(new LineInfo());
						lineInfoList.get(lineInfoList.size()-1).additionalSpaces = this.columnWidth - lengthCounter + wordList.size();
						lineInfoList.get(lineInfoList.size()-1).words = wordList;
						outputList.get(outputList.size()-1).lineInfos = lineInfoList;
					}
					
					//Split
					tmpString = word;
					do {
						if(tmpString.length() > this.columnWidth){
							split = true;
						} else {
							split = false;
						}
						
						// Add Word
						wordList = new Words();
						if(split) {
							wordList.add(tmpString.substring(0, this.columnWidth - 1) + "-");
							tmpString = tmpString.substring(this.columnWidth - 1);
							
							lineInfoList.add(new LineInfo());
							lineInfoList.get(lineInfoList.size()-1).additionalSpaces = 0;
							lineInfoList.get(lineInfoList.size()-1).words = wordList;

							lengthCounter = 0;
						} else {
							wordList.add(tmpString);
							lengthCounter = tmpString.length()+1;
						}
						
					}
					while(split);
					
					
				} else {
					if((word.length() + lengthCounter) > this.columnWidth) {
						// New Line update Last Line Info List
						lineInfoList.add(new LineInfo());
						lineInfoList.get(lineInfoList.size()-1).additionalSpaces = this.getNumberOfAdditionalSpaces(wordList);
						lineInfoList.get(lineInfoList.size()-1).words = wordList;
						outputList.get(outputList.size()-1).lineInfos = lineInfoList;
						wordList = new Words();
						lengthCounter = 0;
					} 
					
					//add word to list and update counter
					wordList.add(word);
					lengthCounter += word.length()+1;
				}
			}
			if(lengthCounter != 0) {
				// New Line update Last Line Info List
				lineInfoList.add(new LineInfo());
				lineInfoList.get(lineInfoList.size()-1).additionalSpaces = this.getNumberOfAdditionalSpaces(wordList);
				lineInfoList.get(lineInfoList.size()-1).words = wordList;
				outputList.get(outputList.size()-1).lineInfos = lineInfoList;
				wordList = new Words();
				lineInfoList = new ArrayList<LineInfo>();
				lengthCounter = 0;
			} 
		}
		return outputList;
	}
	
	private int getNumberOfAdditionalSpaces(Words wordList){
		int returnValue = this.columnWidth;
		
		for(String word:wordList) {
			returnValue -= word.length();
			
		}
		
		return returnValue;
	}
	

}
